package start.demo01.erupt;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class ThreadTest extends Thread{
    public static void main(String[] args){
        ThreadLocal<String> local = new ThreadLocal<>();
        local.set("test");
//        ThreadTest t=new ThreadTest();
//        ThreadTest t1=new ThreadTest();
//        ThreadTest t2=new ThreadTest();
//        //join等待当前线程执行
//        //sleep
//        //yield();当前线程回到就绪状态，等待再次调
//        t2.setPriority(Thread.MAX_PRIORITY);//设置线程优先级
//        t.setPriority(1);
//        t.setDaemon(true);//守护线程，可设置无限循环，其他线程结束了他也会结束
//        //
//        t.start();
//        t1.start();
//        t2.start();
        t0();
    }
    public static void test(){
        new Thread(()->System.out.println("线程执行")).start();
    }
    @Override
    public void run(){
        System.out.println(Thread.currentThread().getName());
    }

//    @Test //不能在此注解中使用sleep
    private static List<String> list=new ArrayList<>();
    public static void t0(){
//        List<String> list=new ArrayList<>();
        list.add("111");
        list.add("221");
        list.add("33");
        list.add("444444");
        list.add("55555");
        List<String> list2=list;
        Thread th = new Thread(() -> {
            System.out.println("开始读");
            for (int i = 0; i < list2.size(); i++) {
                String x = list2.get(i);
                System.out.println(x);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        th.start();
        new Thread(()->{
            list=new ArrayList<>();
            System.out.println("已清理"+list.size());
        }).start();
    }
}
